var treeObj;
var chartEC;

$(function() {
	tree_init();
	checkboxs_init();

	$("#chart-select").change(function() {
		
		if ($("#chartStyle-select").children('option:selected').val() == "stack") {
			//设置归一化
			normalChange(0);
			
		}else if ($("#chartStyle-select").children('option:selected').val() == "line") {
			
			// 设置属性框可选情况
			GetCheckboxsData($(this).val());
			
			// 设置归一化可选情况
			if ($(this).val() < 3) {
				normalChange(1);
			}else {
				normalChange(3);
			}
			
		}

		isChartButtonShow();

	});
	
	$("#chartStyle-select").change(function() {

		// 设置属性可选情况
		chartStyleChange();
		isChartButtonShow();
		
	});
});

// tree初始化
var tree_init = function() {
	console.log(new Date);
	// 获取油气井数据
	$.ajax({
		url : './tree/tree',
		dataType : 'json'
	}).done(function(data) {
		if (!data) {
			return;
		}

		// 初始化树
		var settings = {
			treeId : "mytree",
			check : {
				enable : true,
				chkboxType : {
					"Y" : "",
					"N" : "s"
				}
			},
			view : {
				showIcon : false,
				fontCss : {
					font_size : "13px"
				}
			},
			callback : {
				onClick : myOnClick,
				onCheck : myOnCheck
			}
		};

		treeObj = $.fn.zTree.init($('#xzqtree'), settings, data);
		console.log(new Date);

		// 节点点击的处理
		function myOnClick(event, treeId, treeNode) {
			// alert(treeNode.wellcode+"||"+treeNode.text+"||"+treeNode.id);

			if (treeNode.chkDisabled == false) {

				if (treeNode.checked == true) {
					treeNode.checked = false;
				} else {
					treeNode.checked = true;
				}
				treeObj.updateNode(treeNode);
				isChartButtonShow();
			}

		};
		// 节点checked的处理
		function myOnCheck(event, treeId, treeNode) {
			// alert(treeNode.wellcode+"||"+treeNode.text+"||"+treeNode.id);
			isChartButtonShow();
		};
		chartStyleChange();

	});
}

var checkboxs_init = function(level) {
	GetCheckboxsData(3);
}

var GetCheckboxsData = function(level) {

	// 初始化checkboxs数据
	$.ajax({
		url : './tree/checkbox',
		data : {
			level : level
		},
		dataType : 'json'
	}).done(
			function(data) {
				var tableString = "";

				$.each(data, function(i, value) {

					if (i % 2 == 0) {

						tableString += "<tr><td><input class='chart-checkbox' type='"
								+ "checkbox'" + "name='' id='" + value.code + "-check'"
								+ "value='" + value.code + "' /><label for='" + value.code
								+ "-check'>" + value.name + "</td>";
					} else {
						tableString += "<td><input class='chart-checkbox' type='"
								+ "checkbox'" + "name='' id='" + value.code + "-check'"
								+ "value='" + value.code + "' /><label for='" + value.code
								+ "-check'>" + value.name + "</td></tr>";
					}

				});
				$('#left-property-table').html("");
				$('#left-property-table').append(tableString);
				
				var selected = $("#chart-select").children('option:selected').val(); 

			});
}

var isChartButtonShow = function() {
	var showButton = false;
	var level = $("#chart-select").children('option:selected').val();

	if (treeObj != null) {
		if (level != -1) {
			var nodes = treeObj.getCheckedNodes(true);
			// alert("length："+nodes.length);
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].level == level) {
					showButton = true;
					break;
				}
			}

			if (showButton) {
				$("#showChartBtn").removeAttr("disabled");// 要变成Enable，JQuery只能这么写
			} else {
				$("#showChartBtn").attr("disabled", "disabled");// 再改成disabled
			}

		} else {
			var nodes = treeObj.getNodes();
			var nodes_array = treeObj.transformToArray(nodes);
			for (var i = 0; i < nodes_array.length; i++) {
				treeObj.checkNode(nodes_array[i], false, true);
				treeObj.setChkDisabled(nodes_array[i], true);
			}
		}
	}
}

var chartStyleChange = function() {

	var style = $("#chartStyle-select").children('option:selected').val();

	if (style == "stack") {
		var level = $("#chart-select").children('option:selected').val();
		if (level != -1) {
			
			// 设置属性checkbox 
			$(".chart-checkbox").attr("checked", false);
/*		$("#QQ-check").prop("checked", true);
			$(".chart-checkbox").attr("disabled", "disabled");// 改成disabled
*/
			// 设置作图类型checkbox 除了井组产气曲线其他都不可选
			$("#chart-select").empty();
			$("#chart-select").append(
					"<option value='-1'>按投产时间叠合图</option>"
							+ "<option value='2'>按井组叠合图</option>");
			$("#chart-select").find("option[value='2']").prop("selected", true);
			$("#chart-select").removeAttr("disabled");// 要变成Enable，JQuery只能这么写

			// 设置树结构中除了井组层其他都不可选
			if (treeObj != null) {
				var nodes = treeObj.getNodes();
				var nodes_array = treeObj.transformToArray(nodes);
				for (var i = 0; i < nodes_array.length; i++) {
					if (nodes_array[i].level != 2) {
						treeObj.setChkDisabled(nodes_array[i], true);
					} else {
						treeObj.setChkDisabled(nodes_array[i], false);
						treeObj.expandNode(nodes_array[i], false, true, true);
						treeObj.checkNode(nodes_array[i], true, true);
					}
				}

			}
		} else {
			var nodes = treeObj.getNodes();
			var nodes_array = treeObj.transformToArray(nodes);
			for (var i = 0; i < nodes_array.length; i++) {
				treeObj.checkNode(nodes_array[i], false, true);
				treeObj.setChkDisabled(nodes_array[i], true);
			}
		}

		GetCheckboxsData(0);
		normalChange(0);
		
	} else {

		$(".chart-checkbox").attr("checked", false);
//		$(".chart-checkbox").removeAttr("disabled");// 要变成Enable，JQuery只能这么写

		$("#chart-select").empty();
		$("#chart-select").append(
				"<option value='3'>单井产气曲线</option>"
						+ "<option value='2'>井组产气曲线</option>"
						+ "<option value='1'>区块产气曲线</option>"
						+ "<option value='0'>气田总产气曲线</option>");
		 $("#chart-select").find("option[value='3']").prop("selected", true);
		$("#chart-select").removeAttr("disabled");// 要变成Enable，JQuery只能这么写

		// 设置树结构中除了井组层其他都不可选
		if (treeObj != null) {

			var nodes = treeObj.getNodes();
			var nodes_array = treeObj.transformToArray(nodes);
			for (var i = 0; i < nodes_array.length; i++) {
				var nodes2 = treeObj.getNodesByParam("name", "谭坪外围", null);
				if (nodes2.length == 1) {
					treeObj.expandNode(nodes2[0], true, false, true);
				}
				nodes_array[i].checked = false;
				treeObj.setChkDisabled(nodes_array[i], false);
			}

		}
		
		GetCheckboxsData(3);
		normalChange(3);
	}
}

// 设置归一化单选按钮是否可选的情况,level以上的不可选，level以下包括level可选
var normalChange = function(level) {

	// 恢复为默认的无归一的选中情况
	$('#normal-select input[name="normal"]:eq(0)').prop("checked", true);

	// 大于level的设置为不可点击
	$('#normal-select input[name="normal"]:gt(' + level + ')').attr("disabled",
			"disabled");
	$('#normal-select label:gt(' + level + ')').css("color",
	"grey");
	
	// 小于等于level的设置为可点击
	$('#normal-select input[name="normal"]:eq(' + level + ')').removeAttr(
			"disabled");
	$('#normal-select label:eq(' + level + ')').css("color",
	"black");
	$('#normal-select input[name="normal"]:lt(' + level + ')').removeAttr(
			"disabled");
	$('#normal-select label:lt(' + level + ')').css("color",
	"black");
}
